#!/bin/bash
. ./var
. ./Tmpdir
py3(){
    Python_version="Python-3.8.2.tar.xz"
    TAR_version="Python-3.8.2"

    #调用Tmpdir函数
    Tmpdir
    cd $dir && wget -V &> /dev/null || yum -y install wget
    python3.8 -V &> /dev/null && echoRed "检测到系统中有python3.8命令，故而退出！" && rm -rf $dir && exit 1
    python3 -V &> /dev/null && echoRed "检测到系统中有python3命令，故而退出！" && rm -rf $dir && exit 1
    
    yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel wget gcc gcc-c++ libffi-dev

    #https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tar.xz
    wget $ip/${Python_version} && tar -xf ${Python_version} && mv ${TAR_version} /usr/local/${TAR_version} && cd /usr/local/${TAR_version} && ./configure --prefix=/usr/local && make && make altinstall && make clean && make distclean
    if [ $? = 0 ];then
       echoGreen "python3.8编译安装成功，接下来替换linux下自带的python2"
    
       cd /usr/bin && mv python python.backup && ln -s /usr/local/bin/python3.8 /usr/bin/python && ln -s /usr/local/bin/python3.8 /usr/bin/python3
       [ -f /usr/bin/pip ] && mv /usr/bin/pip /usr/bin/pip.backup && ln -s /usr/local/bin/pip3.8 /usr/bin/pip
       #修改yum配置文件
       cp /usr/bin/yum /usr/bin/yum.backup && cp /usr/libexec/urlgrabber-ext-down /usr/libexec/urlgrabber-ext-down.backup
       sed -i '1s/python/python2/g' /usr/bin/yum && sed -i '1s/python/python2/g' /usr/libexec/urlgrabber-ext-down
       /usr/local/bin/python3.8 -V && echoGreen "已完成安装，可尽情享用！" || echoYellow "可能安装有问题，请检查……"
       rm -rf $dir
    else
        echoRed "python3编译安装失败，即将退出，请检查"
        rm -rf $dir
    fi
}
